//
//  EnemyObj.cpp
//  NinjaTurtle
//
//  Created by Nguyen Tri Chung on 2/6/15.
//
//

#include "EnemyObj.h"

EnemyObj::EnemyObj(const string& identifier) {
    CSprite* mainFace = new CSprite(SPRITE_TYPE_MAIN);
    mainFace->initWithIdentifier(identifier);
    this->sprites->pushBack(mainFace);
    
    RectBody* body = new RectBody(mainFace->getContentSize().width, mainFace->getContentSize().height);
    CC_SAFE_RETAIN(body);
    this->body = body;
    
    this->initEnemy();
}

EnemyObj::EnemyObj(const string& identifier, const string& animation) {
    CSprite* mainFace = new CSprite(SPRITE_TYPE_MAIN);
    mainFace->initWithIdentifier(identifier);
    mainFace->bind(CStringUtil::getPattern(animation));
    this->sprites->pushBack(mainFace);
    
    RectBody* body = new RectBody(mainFace->getContentSize().width, mainFace->getContentSize().height);
    CC_SAFE_RETAIN(body);
    this->body = body;
    
    this->initEnemy();
}

EnemyObj::EnemyObj(const string& identifier, const vector<string>& animations) {
    CSprite* mainFace = new CSprite(SPRITE_TYPE_MAIN);
    mainFace->initWithIdentifier(identifier);
    for (auto animation = animations.begin(); animation < animations.end(); animation++) {
        string animationIdentifier = *animation;
        mainFace->bind(CStringUtil::getPattern(animationIdentifier));
    }
    this->sprites->pushBack(mainFace);
    
    RectBody* body = new RectBody(mainFace->getContentSize().width, mainFace->getContentSize().height);
    CC_SAFE_RETAIN(body);
    this->body = body;
    
    this->initEnemy();
}

void EnemyObj::initEnemy() {
    this->enemyName = "";
    this->equipable = false;
    this->trappable = false;
    this->walkable = false;
    this->machinable = false;
    this->crusherable = false;
    this->aimable = false;
    this->fireable = false;
    this->moveState = MOVE_NONE;
    this->moveSpeed = CONFIG_MOVE_SPEED;
    this->visualRange = new RectBody(CONFIG_VISUAL_RANGE_ENEMY_WIDTH, CONFIG_VISUAL_RANGE_ENEMY_HEIGHT);
    CC_SAFE_RETAIN(this->visualRange);
    
    this->currentElapsedJumpTime = 0;
    this->currentDelayToFire = 0;
    this->currentDelayToCrush = 0;
    this->maxDelayToCrush = 0;
    this->fireX = 0;
    this->fireY = 0;
    this->lastStepY = -9999;
}

EnemyObj::~EnemyObj() {
    CC_SAFE_RELEASE(this->visualRange);
}

void EnemyObj::update(float dt) {
    Obj::update(dt);
    if (this->moveState == MOVE_LEFT) {
        this->body->setVelocity(Point(-this->moveSpeed, 0));
        this->body->setFlipX(true);
    } else if (this->moveState == MOVE_RIGHT) {
        this->body->setVelocity(Point(this->moveSpeed, 0));
        this->body->setFlipX(false);
    } else {
        this->body->setVelocity(Point(0, 0));
    }
}

bool EnemyObj::fire(float dt) {
    if (this->currentDelayToFire > 0) {
        this->currentDelayToFire -= dt;
        if (this->currentDelayToFire < 0) {
            this->currentDelayToFire = -1;
            return true;
        }
    }
    return false;
}

string EnemyObj::getStateMove() {
    if (this->enemyName == EQUIPMENT_WARRIOR) {
        return FRAME_WARRIOR_MOVE;
    }
    if (this->enemyName == EQUIPMENT_DEATH) {
        return FRAME_DEATH_MOVE;
    }
    if (this->enemyName == EQUIPMENT_FAST) {
        return FRAME_FAST_MOVE;
    }
    if (this->enemyName == EQUIPMENT_STRENGTH) {
        return FRAME_STRENGTH_MOVE;
    }
    if (this->enemyName == EQUIPMENT_NINJA) {
        return FRAME_NINJA_MOVE;
    }
    if (this->enemyName == EQUIPMENT_ANGEL) {
        return FRAME_ANGEL_MOVE;
    }
    return "";
}

string EnemyObj::getStateIdle() {
    if (this->enemyName == EQUIPMENT_WARRIOR) {
        return FRAME_WARRIOR_IDLE;
    }
    if (this->enemyName == EQUIPMENT_DEATH) {
        return FRAME_DEATH_IDLE;
    }
    if (this->enemyName == EQUIPMENT_FAST) {
        return FRAME_FAST_IDLE;
    }
    if (this->enemyName == EQUIPMENT_STRENGTH) {
        return FRAME_STRENGTH_IDLE;
    }
    if (this->enemyName == EQUIPMENT_NINJA) {
        return FRAME_NINJA_IDLE;
    }
    if (this->enemyName == EQUIPMENT_ANGEL) {
        return FRAME_ANGEL_IDLE;
    }
    return "";
}

string EnemyObj::getStateThrow() {
    if (this->enemyName == EQUIPMENT_WARRIOR) {
        return FRAME_WARRIOR_THROW;
    }
    if (this->enemyName == EQUIPMENT_DEATH) {
        return FRAME_DEATH_THROW;
    }
    return "";
}
